﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DatabaseCompare.Model;
using DatabaseCompare.Service.Compare;

namespace DatabaseCompare.ServiceImpl.Compare
{

    /// <summary>
    /// 表比较处理
    /// </summary>
    public class TableCompare :
        CompareAbleCompare<TableInfo>, 
        ICompareAbleCompare<TableInfo, TableCompareResult>
    {

        /// <summary>
        /// 列List的比较类.
        /// </summary>
        private ICompareAbleListCompare<ColumnInfo, ColumnCompareResult> columnListCompare = new ColumnListCompare();



        /// <summary>
        /// 两个表比较.
        /// </summary>
        /// <param name="leftTable"></param>
        /// <param name="rightTable"></param>
        /// <returns></returns>
        public TableCompareResult Compare(TableInfo leftTable, TableInfo rightTable)
        {
            // 比较结果.
            TableCompareResult result = new TableCompareResult();

            // 设置比较参数.
            result.Left = leftTable;
            result.Right = rightTable;


             // 首先简单作非空的判断.
            result.CompareResult = base.CommonCompare(leftTable, rightTable);

            if (result.CompareResult == CommonCompareResult.FullSame)
            {
                // 左右都不为空
                // 需要详细比较.

                // 备注/说明信息
                if (leftTable.Comment != rightTable.Comment)
                {
                    result.CompareResult = CommonCompareResult.Difference;
                    result.CommentDifference = true;
                }



                // 表比较结束了，开始比较列.
                result.ColumnCompareResultList =
                    columnListCompare.Compare(leftTable.SubList, rightTable.SubList);



                // 比较列完毕后，只要列里面，有一个不完全匹配
                // 那么认为表不匹配.
                if (result.ColumnCompareResultList != null
                    && result.ColumnCompareResultList.Count > 0)
                {
                    if (result.ColumnCompareResultList.Count(p => p.CompareResult != CommonCompareResult.FullSame) > 0)
                    {
                        result.CompareResult = CommonCompareResult.Difference;
                    }
                }

            }

            // 返回.
            return result;
        }


    }


}
